<template>
  <div class="app-container bg-gray">
    <el-card class="box-1">
      <div slot="header">
        <span>{{ title }}</span>
      </div>
      <el-form ref="form" :model="form" label-width="200px">
        <el-form-item label="轮播图：">
          <ws-upload-single-img v-model="form['thumbnailId']" :path="form['thumbnailUrl']" height="90" width="160"/>
        </el-form-item>
        <el-form-item label="类型：" prop="mobile">
          <el-radio-group v-model="form['type']" size="small">
            <el-radio :label="5">普通轮播图</el-radio>
            <el-radio :label="1">视频</el-radio>
            <el-radio :label="2">分区</el-radio>
            <el-radio :label="3">UP主</el-radio>
            <el-radio :label="4">网页</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="form['type']!=5" :label="form['type']===1?'视频ID：':form['type']===2?'分区ID':form['type']===3?'UP主ID':'网址'">
          <el-input v-model="form['url']"/>
        </el-form-item>
        <el-form-item label="简介：">
          <el-input v-model="form.brief" type="textarea" rows="4"/>
        </el-form-item>
        <el-form-item label="开始时间：">
          <el-date-picker v-model="form.startTime" type="datetime" value-format="timestamp" placeholder="选择日期" />
        </el-form-item>
        <el-form-item label="结束时间：">
          <el-date-picker v-model="form.endTime" type="datetime" value-format="timestamp" placeholder="选择日期" />
        </el-form-item>
        <el-form-item label="权重：">
          <el-input-number v-model="form.sort"/>
        </el-form-item>
        <div style="text-align:center">
          <el-button type="primary" @click="submitForm('form')">保存</el-button>
          <el-button type="primary" @click="$router.back(-1)">取消</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 页面标题
      title: '',
      form: {
        type: 5
      }
    }
  },
  watch: {
  },
  created() {
    this.title = this.$route.query.title
    if (this.$route.query.id) {
      this.id = this.$route.query.id
      this.getData(this.$route.query.id)
    }
  },
  methods: {
    /**
     *  获取用户信息
     *  接口:'customer/detail/{id}'
     */
    getData(id) {
      const url = '../videoAdmin/carousel/getInfo'
      const data = {
        id: id
      }
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.form = res.data
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = this.form
          const url = this.id ? '../videoAdmin/carousel/updateInfo' : '../videoAdmin/carousel/create'
          this.$store.dispatch('GetConnect', { url, data }).then(res => {
            this.$message.success(res.msg)
            this.$router.back(-1)
          }).catch((err) => {
            this.$message.error(err.msg)
          })
        } else {
          this.$message.error('提交失败,请检查必填项')
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../../styles/index.scss';
.box-1 {
  max-width: 1000px;
  margin: 0 auto;
  > div span {
    font-size: 18px;
    font-weight: 600;
    padding: 0 10px;
  }
  .el-form {
    max-width: 900px;
    .title {
      font-weight: 600;
    }
    .remark {
      font-size: 13px;
      color: $textGray;
    }
    > div > .el-button {
      @extend .bg-green;
      width: 20%;
      min-width: 160px;
    }
  }
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
